﻿using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace SEAM.Data.OleDb
{
    /// <summary>
    /// Summary description for OracleDataAccess.
    /// </summary>
    public class OleDbDataAccess : DataAccess
    {
        private DbConnection _Connection = null;
        private DbTransaction _Transaction = null;

        #region Constructors

        public OleDbDataAccess(string connectionString) :
            base(connectionString) { }

        #endregion

        #region DataAccess Members

        protected override DbConnection Connection
        {
            get
            {
                if (_Connection == null)
                    _Connection = new OleDbConnection(ConnectionString);
                return _Connection; 
            }
        }

        protected override DbTransaction Transaction
        {
            get { return _Transaction; }
            set { _Transaction = value; }
        }

        protected override DbCommand GetCommand(CommandType cmdType, string cmdText, int? iTimeout)
        {
            DbCommand cmdCommand = new OleDbCommand(cmdText, (OleDbConnection)Connection);
            cmdCommand.CommandType = cmdType;
            if (iTimeout.HasValue)
                cmdCommand.CommandTimeout = iTimeout.Value;
            return cmdCommand;
        }

        protected override DbDataAdapter GetAdapter(DbCommand cmdCommand)
        {
            return new OleDbDataAdapter((OleDbCommand)cmdCommand);
        }

        protected override DbParameter AddCommandParameter(DbCommand cmdCommand, DataAccessParam daParameter)
        {
            return ((OleDbCommand)cmdCommand).Parameters.Add(daParameter.Name, GetDBType(daParameter.Type));
        }

        protected override DbParameter SetReturnValueParameter(DbCommand cmdCommand)
        {
            return ((OleDbCommand)cmdCommand).Parameters.Add("@ReturnCode", GetDBType(ParamType.Integer));
        }

        #endregion

        #region Helper Functions

        protected OleDbType GetDBType(ParamType Type)
        {
            switch (Type)
            {
                case ParamType.Boolean:
                    return OleDbType.Binary;
                case ParamType.DateTime:
                    return OleDbType.Date;
                case ParamType.Short:
                    return OleDbType.SmallInt;
                case ParamType.Integer:
                    return OleDbType.Integer;
                case ParamType.Long:
                    return OleDbType.BigInt;
                case ParamType.String:
                    return OleDbType.VarChar;
                case ParamType.Byte:
                    return OleDbType.TinyInt;
                case ParamType.Float:
                    return OleDbType.Double;
                default:
                    throw new DataAccessException("Unknown Parameter Type: " + Type.ToString());
            }
        }

        #endregion

    }
}
